<template>
    <!-- 修改资料 -->
    <div>
                <!-- 内容主题区域 -->
                <el-form label-width="85px" :model="form" label-position="right" ref="formRef" :rules="formRules">
                    <el-form-item label="编号" prop="number">
                        <el-input v-model="form.number"></el-input>
                    </el-form-item>
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                    <el-form-item label="上级类别" prop="parentId">
                        <el-tree-select v-model="form.parentId" :data="parentData" :render-after-expand="false"
                            :check-strictly=true />
                    </el-form-item>
                    <el-form-item label="分类" prop="category" >
                        <el-select v-model="form.category" clearable placeholder="请选择：">
                            <el-option v-for="item in configData.categoryList" :key="item.value" :label="item.label" :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="状态" prop="status">
                        <el-switch v-model="form.status" size="large" inline-prompt :active-icon="Check"
                            :inactive-icon="Close" active-value="normal" inactive-value="disable" />
                    </el-form-item>
                    <el-form-item label="备注">
                        <el-input v-model="form.remark"></el-input>
                    </el-form-item>

                </el-form>
                <div class="button-position">
                    <el-button @click="updateVisible">取 消</el-button>
                    <el-button type="primary" @click="addcodekind(formRef)">确 定</el-button>
                </div>
    </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue';
import { ElNotification} from 'element-plus';
import { Check,Close } from '@element-plus/icons-vue';
import Api from '@/api/model/system'
const emit = defineEmits(['updatetable'])
// 添加用户数据的对象
const form = ref({});
const configData = ref({})
const parentData = ref([])
const formRef = ref()
const formRules = reactive({
    name: [
        { required: true, message: "请输入名称", trigger: "blur" },
    ],
    number: [
        { required: true, message: "请输入编号", trigger: "blur" },
    ]
})

onMounted(() => {
    getInit()
})

const getInit = async () => {
    let params = {
        fileName: 'CodeKindTreeReport'
    }
    let res = await Api.codekind.CodeKindTree.post(params);
    if (res.code == 200) {
        parentData.value = res.reportData
    }
    let formRes = await Api.codekind.add.post()
    if(formRes.code == 200){
        form.value = formRes.data
        configData.value = formRes.configData
    }
}

const restAddFormData = () => {
    formRef.value.resetFields();
}

const updateVisible = (val) => {
    emit('updatetable')
}


// 上传
const addcodekind = async (formRef) => {
    await formRef.validate((valid, fields) => {
        if (valid) {
            addcodekindData();
        }
    })
}

const addcodekindData = async () => {
    var params = {
        bean: form.value,
    }
    var res = await Api.codekind.save.post(params);
    if (res.code == 200) {
        restAddFormData()
        ElNotification.success({ title: '', message: '添加成功！', duration: 2000 });
        emit('updatetable',true)
    }
}

</script>

<style lang="less" scoped>
.el-dialog__wrapper {
    pointer-events: none;
}

.el-dialog {
    pointer-events: auto;
}
</style>

